package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.type.JdbcType;
import org.example.pojo.ItripHotel;
import org.example.vo.detail.DetailHotel;
import org.example.vo.recommend.hotel.Recommend_Hotel;
import org.example.vo.solr.SearchHotel;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface ItripHotelMapper extends BaseMapper<ItripHotel> {
    class ItripHotelProvider {
        public String queryOrderByParam(Map<String, Object> para) {
            StringBuilder sql = new StringBuilder("select ih.id id,ih.hotelName hotelName,ih.hotelLevel hotelLevel,ih.address address,im.imgUrl imgUrl from itrip_hotel ih " +
                    " inner join itrip_image im on ih.id = im.targetId " +
//                    " inner join itrip_hotel_room ihr on ihr.hotelId = ih.id " +
                    " where im.type = '0' AND ");
            String hotelName = para.get("hotelName").toString();
            String redundantCityName =para.get("redundantCityName").toString();
            System.out.println(hotelName+"    "+redundantCityName);
            if ((hotelName == null || hotelName.equals("")) && (redundantCityName == null || redundantCityName.equals(""))){
                sql.append("position = 1");
            }else {
                if (hotelName == null || hotelName.equals("")) {
                    if (redundantCityName != null && !redundantCityName.equals("")) {
                        sql.append("ih.redundantCityName = #{redundantCityName} AND ");
                    }
                } else {
                    if (redundantCityName == null || redundantCityName.equals("")) {
                        sql.append("ih.hotelName like concat('%',#{hotelName},'%') AND ");
                    } else {
                        sql.append("ih.hotelName like concat('%',#{hotelName},'%') AND ih.redundantCityName = #{redundantCityName} AND ");
                    }
                }
                sql.append("position = 1");
            }
            System.out.println(sql);
            return sql.toString();
        }
    }


    @Select("select ihpr.hotelId id,ih.hotelName,ih.hotelLevel,recommendOrder,im.imgUrl,min(ihr.roomPrice) price from itrip_hotel_paidlisting_recommend ihpr\n" +
            "    inner join itrip_hotel ih on ihpr.hotelId = ih.id\n" +
            "    inner join itrip_image im on ihpr.hotelId = im.targetId\n" +
            "    inner join itrip_hotel_room ihr on ihr.hotelId = ihpr.hotelId\n" +
            "where ih.redundantCityName = #{city} and im.type = '0' and position = 1\n" +
            "group by ihr.hotelId,ihpr.cityId\n" +
            "order by ihpr.recommendOrder")
    @Results({
            @Result(column = "id",property = "id",jdbcType = JdbcType.BIGINT,id = true),
            @Result(column = "hotelName",property = "hotelName",jdbcType = JdbcType.VARCHAR),
            @Result(column = "hotelLevel",property = "hotelLevel",jdbcType = JdbcType.INTEGER),
            @Result(column = "recommendOrder",property = "recommendOrder",jdbcType = JdbcType.INTEGER),
            @Result(column = "imgUrl",property = "imgUrl",jdbcType = JdbcType.VARCHAR),
            @Result(column = "price",property = "price",jdbcType = JdbcType.DECIMAL)
    })
    List<Recommend_Hotel> queryByCityName(String city);

    @Select("select ih.id id,ih.hotelName hotelName,ih.hotelLevel hotelLevel,ih.address address,im.imgUrl imgUrl from itrip_hotel ih inner join itrip_image im on ih.id = im.targetId where ih.id = #{id} and im.type = '0' and position = 1")
    @Results({
            @Result(column = "id",property = "id",jdbcType = JdbcType.BIGINT,id = true),
            @Result(column = "hotelName",property = "hotelName",jdbcType = JdbcType.VARCHAR),
            @Result(column = "hotelLevel",property = "hotelLevel",jdbcType = JdbcType.INTEGER),
            @Result(column = "address",property = "address",jdbcType = JdbcType.VARCHAR),
            @Result(column = "imgUrl",property = "imgUrl",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "price",property = "price",jdbcType = JdbcType.DECIMAL)
    })
    SearchHotel queryByHotelId(Long id);


//    @Select("select ih.id id,ih.hotelName hotelName,ih.hotelLevel hotelLevel,ih.address address," +
//            "ih.details details,ih.facilities facilities,ih.hotelPolicy hotelPolicy,ih.redundantCityName redundantCityName," +
//            "ih.redundantHotelStore redundantHotelStore,im.imgUrl imgUrl " +
//            "from itrip_hotel ih inner join itrip_image im on ih.id = im.targetId " +
//            "where ih.id = #{id} and im.type = '0' and position = 1")
//    @Results({
//            @Result(column = "id",property = "id",jdbcType = JdbcType.BIGINT,id = true),
//            @Result(column = "hotelName",property = "hotelName",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "hotelLevel",property = "hotelLevel",jdbcType = JdbcType.INTEGER),
//            @Result(column = "address",property = "address",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "details",property = "details",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "facilities",property = "facilities",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "hotelPolicy",property = "hotelPolicy",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "redundantCityName",property = "redundantCityName",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "redundantHotelStore",property = "redundantHotelStore",jdbcType = JdbcType.INTEGER),
//            @Result(column = "imgUrl",property = "imgUrl",jdbcType = JdbcType.VARCHAR),
//    })
//    DetailHotel queryByHotelDetailId(Long id);


    @SelectProvider(type = ItripHotelProvider.class,method = "queryOrderByParam")
    @Results({
            @Result(column = "id",property = "id",jdbcType = JdbcType.BIGINT,id = true),
            @Result(column = "hotelName",property = "hotelName",jdbcType = JdbcType.VARCHAR),
            @Result(column = "hotelLevel",property = "hotelLevel",jdbcType = JdbcType.INTEGER),
            @Result(column = "address",property = "address",jdbcType = JdbcType.VARCHAR),
            @Result(column = "imgUrl",property = "imgUrl",jdbcType = JdbcType.VARCHAR),
//            @Result(column = "price",property = "price",jdbcType = JdbcType.DECIMAL)
    })
    List<SearchHotel> queryByHotelNameAndCity(@Param("hotelName") String hotelName,@Param("redundantCityName") String redundantCityName);
}
